﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Text;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace shupaitextreader
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        CanvasDrawingSession ds;
        string str = string.Empty;
        Rect rect;
        private void canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
           /// sharedResources = TextDirectionControl.GetSharedResources(sender);

          




            //var arrow = sharedResources.GetArrow(CurrentDirection);

            //var arrowBounds = arrow.ComputeStrokeBounds(4);

            //var destSize = new Size(sender.ActualWidth / 2, sender.ActualHeight / 2);
            //var destBounds = new Rect(new Point(sender.ActualWidth / 2 - destSize.Width / 2, sender.ActualHeight / 2 - destSize.Height / 2), destSize);
            //arrow = arrow.Transform(TextDirectionControl.CalculateTransform(arrowBounds, destBounds));

            //ds.DrawGeometry(arrow, drawingColor, 4, TextDirectionControl.ArrowStrokeStyle);
        }

        void DrawAlignedText(CanvasDrawingSession ds, Rect rect, CanvasHorizontalAlignment horizontalAlignment, CanvasVerticalAlignment verticalAlignment)
        {
            //ds.DrawText(string.Format("{0}-{1}\nAligned", verticalAlignment.ToString(), horizontalAlignment.ToString()),
            //    rect, drawingColor,
            //    new CanvasTextFormat()
            //    {
            //        FontSize = 20,
            //        HorizontalAlignment = horizontalAlignment,
            //        VerticalAlignment = verticalAlignment,
            //        Direction = CurrentDirection
            //    });
        }

        private async void open_click(object sender, RoutedEventArgs e)
        {
            Windows.Storage.Pickers.FileOpenPicker folderPicker = new Windows.Storage.Pickers.FileOpenPicker();
            folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            folderPicker.FileTypeFilter.Add(".txt");
            StorageFile file = await folderPicker.PickSingleFileAsync();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //注册GBK编码 
            Encoding encodingGbk = Encoding.GetEncoding("GBK");
            if (file != null)
            {
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                {
                    StreamReader reader = new StreamReader(stream.AsStreamForRead(), encodingGbk);
                    str=reader.ReadToEnd();
                }
                CanvasControl canvas = new CanvasControl();
                canvas.Draw += Canvas_Draw;
                lay.Children.Add(canvas);
                Grid.SetRow(canvas, 1);
               
            }
        }

        private void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            
            ds = args.DrawingSession;
            ds.Clear(Colors.White);
            rect = new Rect(-200,100, sender.ActualWidth, sender.ActualHeight-100);
            ds.DrawText(str, rect, Colors.Indigo,
                new CanvasTextFormat()
                {
                    FontSize = 24,    
                    LineSpacing=50,
                    FontFamily= "华文细黑",
                    HorizontalAlignment = CanvasHorizontalAlignment.Left,
                    VerticalAlignment = CanvasVerticalAlignment.Top,
                    Direction = CanvasTextDirection.TopToBottomThenRightToLeft,
                   WordWrapping=CanvasWordWrapping.Wrap,
                    LastLineWrapping=true,
                     //LineSpacingMode=CanvasLineSpacingMode.Proportional,
                      OpticalAlignment=CanvasOpticalAlignment.NoSideBearings,
                       TrimmingSign=CanvasTrimmingSign.Ellipsis
                });
        }
    }
}
